package com.lcj.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcj.commonutils.R;
import com.lcj.eduService.entity.EduChapter;
import com.lcj.eduService.entity.EduCourse;
import com.lcj.eduService.entity.EduTeacher;
import com.lcj.eduService.entity.vo.CourseInfoVo;
import com.lcj.eduService.entity.vo.CoursePublishVo;
import com.lcj.eduService.entity.vo.CourseQuery;
import com.lcj.eduService.entity.vo.TeacherQuery;
import com.lcj.eduService.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-14
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class EduCourseController {


    @Autowired
    private EduCourseService eduCourseService;

    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id=eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCouseInfo")
    public R updataCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    @PostMapping("publishCourse/{id}")
    public  R publishCourse(@PathVariable String id){
        EduCourse eduCourse =new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表
    /**
     * 条件查询
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,
                             @PathVariable long limit,
                             @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageCourse=new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //判断条件是否为空，如果不为空拼接条件
        String title=courseQuery.getTitle();
        String teacherId=courseQuery.getTeacherId();
        String subjectParentId=courseQuery.getSubjectParentId();
        String subjectId=courseQuery.getSubjectId();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }

        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }

        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.ge("subject_parent_id",subjectParentId);
        }

        if(!StringUtils.isEmpty(subjectId)){
            wrapper.le("subject_id",subjectId);
        }

        wrapper.orderByDesc("gmt_create");
        eduCourseService.page(pageCourse,wrapper);
        long total = pageCourse.getTotal();
        List<EduCourse> records=pageCourse.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("row",records);
        return R.ok().data(map);
    }

    //删除课程
    @DeleteMapping("delete/{courseId}")
    public R deleteCourseById(@PathVariable String courseId){
        boolean result = eduCourseService.deleteCourseById(courseId);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

